﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Kawanon.Localization;

namespace Kawanon.QtHoney
{
	public partial class ChooseFromArrayForm : Form
	{
		public ChooseFromArrayForm()
		{
			InitializeComponent();

			this.Text = StringResource.Localize(MiscResources.ChooseFromArrayForm_Text);
		}

		private object[] m_source;

		public object[] Source
		{
			get
			{
				return m_source;
			}
			set
			{
				m_source = value;

				//UpdateItems();
			}
		}

		public Converter<object, string> Stringizer { get; set; }

		public object Selected { get; private set; }

		private void UpdateItems()
		{
			listView1.SuspendLayout();
			listView1.Items.Clear();

			Array.ForEach(m_source,
				o => listView1.Items.Add(Stringizer != null ? Stringizer(o) : o.ToString()));

			listView1.ResumeLayout();
		}

		private void listView1_ItemActivate(object sender, EventArgs e)
		{
			ListViewItem item = listView1.FocusedItem;
			if (item == null)
			{
				Dbg.Play();
				return;
			}
			Selected = m_source[item.Index];

			this.DialogResult = DialogResult.OK;
			Close();
		}

		private void ChooseFromArrayForm_Load(object sender, EventArgs e)
		{
			UpdateItems();
		}
	}
}
